package command;

/**
 * 停止设备命令
 */
public class StopCommand implements Command {
    private final AgriculturalDevice device;
    private boolean wasRunning = false; // 记录执行前的状态

    public StopCommand(AgriculturalDevice device) {
        this.device = device;
    }

    @Override
    public void execute() {
        wasRunning = device.isRunning();
        if (wasRunning) {
            System.out.println("停止设备: " + device.getDeviceId());
            device.stop();
        } else {
            System.out.println("设备已停止: " + device.getDeviceId());
        }
    }

    @Override
    public void undo() {
        if (wasRunning) { // 如果执行前设备在运行，则撤销时启动设备
            System.out.println("撤销停止: " + device.getDeviceId());
            device.start();
        }
    }
}
